package com.foreknow.service.impl;

import com.foreknow.dao.impl.GuestbookDaoImpl;
import com.foreknow.db.DBManager;
import com.foreknow.model.Guestbook;
import com.foreknow.service.GuestbookService;
import com.foreknow.util.Pagination;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class GuestbookServiceImpl implements GuestbookService {

    /**
     * 添加留言的方法
     * @param guestbook
     * @return
     */
    @Override
    public boolean save(Guestbook guestbook) {
        DBManager dbManager = DBManager.getInstance();
        //连接数据库
        Connection conn = dbManager.getConnection();
        GuestbookDaoImpl guestbookDao = new GuestbookDaoImpl();
        //将DBManager中获取到的连接传递给BaseDAO中的setConnection(conn)
        guestbookDao.setConnection(conn);
        boolean isRight = false;
        try {
            isRight = guestbookDao.addMessage(guestbook);
            //提交事物
            conn.commit();
        } catch (SQLException e) {
            try {
                conn.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
            e.printStackTrace();
        }
        return isRight;
    }

    /**
     * 查询
     * @return
     */
    @Override
    public List<Object> queryInfo() {
        //连接数据库
        DBManager dbManager = DBManager.getInstance();
        Connection conn = dbManager.getConnection();
        GuestbookDaoImpl guestbookDao = new GuestbookDaoImpl();
        guestbookDao.setConnection(conn);
        List<Object> list = null;
        list = guestbookDao.getQueryAll();
        return list;
    }

    /**
     * 根据id删除
     * @param id
     * @return
     */
    @Override
    public boolean deleteBean(Integer id) {
        DBManager dbManager = DBManager.getInstance();
        //连接数据库
        Connection conn = dbManager.getConnection();
        GuestbookDaoImpl guestbookDao = new GuestbookDaoImpl();
        //将DBManager中获取到的连接传递给BaseDAO中的setConnection(conn)
        guestbookDao.setConnection(conn);
        boolean isRight = false;
        try {
            isRight = guestbookDao.deleteById(id);
            //提交事物
            conn.commit();
        } catch (SQLException e) {
            try {
                conn.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
            e.printStackTrace();
        }
        return isRight;
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @Override
    public Guestbook idQueryInfo(Integer id) {
        //连接数据库
        DBManager dbManager=DBManager.getInstance();
        Connection conn= dbManager.getConnection();
        GuestbookDaoImpl guestbookDao=new GuestbookDaoImpl();
        guestbookDao.setConnection(conn);
        Guestbook guestbook = guestbookDao.getIdQuery(id);
        return guestbook;
    }

    /**
     * 修改信息
     * @return
     */
    @Override
    public boolean updateBean(Guestbook gb) {
        //连接数据库
        DBManager dbManager=DBManager.getInstance();
        Connection conn = dbManager.getConnection();
        GuestbookDaoImpl guestbookDao=new GuestbookDaoImpl();
        guestbookDao.setConnection(conn);
        boolean isRight=false;
        try {
            isRight = guestbookDao.updateInfo(gb);
            conn.commit();
        } catch (SQLException e) {
            try {
                conn.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
            e.printStackTrace();
        }
        return isRight;
    }

    /**
     *分页查询
     * @param pageSize
     * @param pageNumber
     * @return
     */
    @Override
    public Pagination getByPage(int pageSize, int pageNumber) {
        //连接数据库
        DBManager dbManager=DBManager.getInstance();
        Connection conn=dbManager.getConnection();
        GuestbookDaoImpl guestbookDao=new GuestbookDaoImpl();
        guestbookDao.setConnection(conn);
        Pagination pagination=null;
        try {
            pagination= guestbookDao.getGuestbookByPage(pageSize, pageNumber);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return pagination;
    }
}
